package casa.depesos.app.com.http.req;


import java.io.File;

import casa.depesos.app.com.http.HttpReq;
import casa.depesos.app.com.http.Req;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.data.UploadType;
import casa.depesos.app.com.http.parser.GetCodeParser;
import casa.depesos.app.com.utils.Constant;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UploadReq implements HttpReq<GetCodeBean> {

    String filePath;
    UploadType type;

    public UploadReq(UploadType type, String path) {
        filePath = path;
        this.type = type;
    }

    private String getUrl() {
        switch (type) {
            case ID_FRONT:// /app/orcAndFace/ocrFrontImage
                return "/app/ocrAndBiometric/scanPrimaryDocumentImage";
            case ID_BACK:// /app/orcAndFace/ocrBackImage
                return "/app/ocrAndBiometric/scanSecondaryDocumentImage";
            case FACE://     /app/orcAndFace/faceImage
                return "/app/ocrAndBiometric/captureFacialImage";
            default:
                return "";
        }
    }

    private String getKey() {
        switch (type) {
            case ID_FRONT:
                return "condFlag";
            case ID_BACK:
                return "alt";
            case FACE:
                return "fph";
        }
        return "";
    }

    @Override
    public void req(ReqCallback<GetCodeBean> callback) {
        File file = new File(filePath);
        // 请求体
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("reqnum", Constant.getApplyId())
                .addFormDataPart(
                        getKey(),
                        file.getName(),
                        MultipartBody.create(MediaType.parse("image/jpeg"), file)
                ).build();
        Req.post(getUrl(), getParam(), getHeader(), body, new GetCodeParser(), callback, this);
    }
}
